﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;
using System.Web.Security;
using System.Collections;
using System.Drawing;
using SubSonic;

public partial class ExpertUsers_RequestDevice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtTime.Text = DateTime.Now.ToString();
         string TodayDate = DateTime.Now.Date.ToShortDateString();
         Session["DateToday"] = TodayDate + " 00:00:00.000";

        GwListTask.DataBind();
        GwRequestDevice.DataBind();

        IDataReader Dr = null;
        var thisUser = Membership.GetUser(User.Identity.Name);
        if (!IsPostBack)
        {
            Dr =TblExpert.FetchByParameter(TblExpert.Columns.UserName, SubSonic.Comparison.Equals, Session["UserName"].ToString());
            if (Dr.Read())
            {

                Session["ExID"] = Dr["Id"].ToString();
            }
            Dr.Close();
        }
       
        lblmessage.Text = "";
        Lblmessage2.Text = "";

    }
    protected void GwListTask_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "RequestDevice") {
            PnlRequestDevice.Visible = true;
            lblNumCust.Text = e.CommandArgument.ToString();
            Session["CustFolderId"] = e.CommandArgument.ToString();
            
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        

         int DeviceID = Convert.ToInt32(DrDevice.SelectedValue.ToString());
       

          SubSonic.SqlQuery q3 = new SubSonic.Select(TblRepository.Columns.DeviceId,TblReposDetail.Columns.Id)
          .From(Tables.TblReposDetail)
          .InnerJoin("TblRepository", TblRepository.Columns.Id,"TblReposDetail", TblReposDetail.Columns.IdRepos)
          .Where("DeviceId").IsEqualTo(DeviceID).And("Available").IsEqualTo("True");

         IDataReader DeatR = q3.ExecuteReader();
         int Id = 0;
         if (Convert.ToInt32(q3.GetRecordCount()) == 0)
         {
             lblmessage.ForeColor = Color.Red;
             lblmessage.Text = "کالای انتخاب شده در انبار موجود نمی باشد ";

         }
         else
         {
             int CountDevice=Convert.ToInt32(txtCount.Text.ToString());

             for (int i =1 ; i <=CountDevice ; i++)
             {
                 if (DeatR.Read())
                 {
                     TblDeviceExpert DeviceE = new TblDeviceExpert();
                     DeviceE.ExpertId = Convert.ToInt32(Session["ExID"].ToString());
                     DeviceE.DateRequest = Convert.ToDateTime(txtTime.Text);
                     DeviceE.CustFolderId = Convert.ToInt32(Session["CustFolderId"].ToString());
                     DeviceE.Count = 1;

                     Id = Convert.ToInt32(DeatR["id"].ToString());
                     DeviceE.ReposDetailsId = Id;
                     SubSonic.Query Query = TblReposDetail.CreateQuery();
                     Query.QueryType = SubSonic.QueryType.Update;
                     Query.AddUpdateSetting(TblReposDetail.Columns.Available, "False");
                     Query.WHERE(TblReposDetail.Columns.Id, Id);
                     Query.Execute();
                     DeviceE.Save();
                     lblmessage.Text = "کالا با موفقیت ثبت شد";
                     lblmessage.ForeColor = Color.Green;

                 }
                 else
                 {
                     lblmessage.ForeColor = Color.Red;
                     lblmessage.Text = "کالای انتخاب شده در انبار موجود نمی باشد";
                 }
             }

            
             GwRequestDevice.DataBind();
             txtCount.Text = "";
         }
        
    
       


     
    }
    protected void GwRequestDevice_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       

        if (e.CommandName == "DeleteRequest")
        {

            int  IdReposDEtails = Convert.ToInt32(e.CommandArgument.ToString());
            SubSonic.Query Query = TblReposDetail.CreateQuery();
            Query.QueryType = SubSonic.QueryType.Update;
            Query.AddUpdateSetting(TblReposDetail.Columns.Available, "True");
            Query.WHERE(TblReposDetail.Columns.Id, IdReposDEtails);
            Query.Execute();

            TblDeviceExpert.Delete("ReposDetailsId", IdReposDEtails);
            Lblmessage2.Text = "حذف درخواست کالا با موفقیت انجام شده";
            GwRequestDevice.DataBind();
        }
    }
}